import ckeditor.fields
from django.db import models
from user.models import CustomUser


# Create your models here.

class Categorize(models.Model):
    title = models.CharField(max_length=100, verbose_name="种类名")
    parent = models.ForeignKey(max_length=100, verbose_name="父类名", to="Categorize", on_delete=models.CASCADE,
                               blank=True, null=True)

    def __str__(self):
        return self.title

    def children(self):
        return Categorize.objects.filter(parent=self)


class Tag(models.Model):
    title = models.CharField(max_length=100, verbose_name="标签")

    def __str__(self):
        return self.title


class Article(models.Model):
    title = models.CharField(max_length=100, verbose_name="标题")
    text = ckeditor.fields.RichTextField(verbose_name='文章')
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    change_time = models.DateTimeField(auto_now=True, verbose_name='最后修改时间')
    categorizes = models.ManyToManyField(verbose_name="所属分类", to=Categorize)
    tags = models.ManyToManyField(verbose_name="标签", to=Tag)
    author = models.ForeignKey(verbose_name="作者", to=CustomUser, on_delete=models.CASCADE, null=True)
    state = models.CharField(verbose_name="状态", max_length=15, choices=(
        ("success", "已通过"),
        ("fail", "已驳回"),
        ("nodo", "未审核")
    ), default="nodo")

    def __str__(self):
        return f"{self.title}\t作者:{self.author}"

    class Meta:
        ordering = ['-create_time']


class Comment(models.Model):
    user = models.ForeignKey(to=CustomUser,verbose_name="用户",on_delete=models.CASCADE)
    article = models.ForeignKey(to=Article,verbose_name="文章",on_delete=models.CASCADE)
    content = models.TextField(verbose_name="评论")
    parent = models.ForeignKey(to="Comment",verbose_name="父评论",on_delete=models.CASCADE,blank=True, null=True)
    create_time = models.DateTimeField(auto_now_add=True,verbose_name="创建时间")
    class Meta:
        ordering = ['-create_time']

    def __str__(self):
        return f"{self.user.username}\t{self.article.title}\t{self.content}"


